package com.zgh.myexamproject;


import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.zgh.myexamproject.bean.Game;
import com.zgh.myexamproject.util.MyDBHelper;


@SuppressLint("Registered")
public class DeleteGamesActivity extends AppCompatActivity  implements View.OnClickListener{


    private EditText etGameid;
    private Button btnSearch;
    private EditText etGamename;
    private EditText etGametime;
    private EditText etGamenote;
    private Button btnDelete;
    private SQLiteDatabase db;
    private MyDBHelper myDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_games);
        myDBHelper = new MyDBHelper(this);
        initView();
    }

    private void initView() {
        etGameid=(EditText) findViewById(R.id.et_gameid);
        btnSearch=(Button) findViewById(R.id.btn_search);
        etGamename=(EditText)findViewById(R.id.et_gamename);
        etGametime=(EditText)findViewById(R.id.et_gametime);
        etGamenote=(EditText)findViewById(R.id.et_gamenote);
        btnDelete= (Button) findViewById(R.id.btn_delete);
        btnSearch.setOnClickListener((View.OnClickListener) this);
        btnDelete.setOnClickListener((View.OnClickListener) this);

    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_search:
                searchOrder();
                break;
            case R.id.btn_delete:
                deleteOrder();
                break;
        }//974296731
        //443211123123
    }
    private void searchOrder() {
        String gameid=etGameid.getText().toString().trim();
        db = myDBHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from tb_Game where gameid = ?", new String[]{gameid});
        Game game = new Game();
        if(cursor.getCount()==0){
            Toast.makeText(this, "没有此游戏!", Toast.LENGTH_SHORT).show();
        }
        else{
            cursor.moveToFirst();
            game.gameid = cursor.getString(cursor.getColumnIndex("gameid"));
            game.gamename = cursor.getString(cursor.getColumnIndex("gamename"));
            game.gametime = cursor.getString(cursor.getColumnIndex("gametime"));
            game.gamenote = cursor.getString(cursor.getColumnIndex("gamenote"));
        }
        etGamename.setText(game.gamename);
        etGametime.setText(game.gametime);
        etGamenote.setText(game.gamenote);
        cursor.close();
        db.close();
    }
    private void deleteOrder() {
        Game game=new Game();
        game.gameid=etGameid.getText().toString().trim();
        db = myDBHelper.getWritableDatabase();
        int result= db.delete("tb_Game","gameid=?",new String[]{game.gameid});
        if(result>0) {
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
        }
        db.close();
    }
}
